#include "Domain.h"
#include "CanonicalConv.h"
#include <time.h>
#include <string>
#include <iostream>
#include <fstream>
#include "md5/md5.h"

using namespace std;


Domain::Domain() : _hasRobots(false), _fetchRobots(false)
{
}

Domain::Domain(string dom) :
	_domain(dom), _whenAvailable(time(&_whenAvailable)), _hasRobots(false), _fetchRobots(false)
{	
}

void Domain::setHasRobots()
{
	_hasRobots = true;
}

bool Domain::getHasRobots()
{
	return _hasRobots;
}

bool Domain::getFetchRobots()
{
	return _fetchRobots;
}

void Domain::setFetchRobots()
{
	_fetchRobots =  true;
}

Domain::~Domain()
{
}

string Domain::getDomain()
{
	return _domain;
}

void Domain::setDomain(string dom)
{
	_domain = dom;
}

time_t Domain::getWhenAvailable()
{
	return _whenAvailable;
}

void Domain::setWhenAvailable(time_t time)
{
	_whenAvailable=time;
}


/*void Domain::addSite(string urlSite)
{
	char* temp = const_cast<char*>(urlSite.c_str()) ;
	string urlMD5 = MD5String(temp);
	sites.insert(urlMD5);
} */

//bool Domain::hasSite(Site &site)
//{
//	return (sites.find(site.getUrl())!=sites.end());
//}


string Domain::getTrueDomain(string urlSite)
{
	string domain = CanonicalConv::getCanonicalDomain(urlSite);
	int lastPos = 0;
	if(domain.find(".com.br")!= string::npos)
	{
		string aux = domain.substr(0,domain.find(".com.br"));
		int pos = aux.rfind('.');
		if( (pos+1) < domain.length())
			return domain.substr(pos+1);
	}
	if(domain.find(".org.br")!= string::npos)
	{
		string aux = domain.substr(0,domain.find(".org.br"));
		int pos = aux.rfind('.');
		if( (pos+1) < domain.length())
			return domain.substr(pos+1);
	}
	if(domain.find(".com")!= string::npos)
	{
		string aux = domain.substr(0,domain.find(".com"));
		int pos = aux.rfind('.');
		if( (pos+1) < domain.length())
			return domain.substr(pos+1);
	}
	if(domain.find(".br")!= string::npos)
	{
		string aux = domain.substr(0,domain.find(".br"));
		int pos = aux.rfind('.');
		if( (pos+1) < domain.length())
			return domain.substr(pos+1,domain.size()-pos);
	}
	if(domain.find(".org")!= string::npos)
	{
		string aux = domain.substr(0,domain.find(".org"));
		int pos = aux.rfind('.');
		if( (pos+1) < domain.length())
			return domain.substr(pos+1);
	}
	
	return domain;
}
